package com.sdgc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sdgc.mapper.EmpExprMapper;
import com.sdgc.mapper.EmpMapper;
import com.sdgc.pojo.Emp;
import com.sdgc.pojo.EmpExpr;
import com.sdgc.pojo.EmpQueryParam;
import com.sdgc.pojo.PageResult;
import com.sdgc.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {


    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    //-------------原始分页操作代码----------------------------------------------------
//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize) {
//
////        1.调用mapper接口，查询总记录数
//        Long total = empMapper.count();
////        2.调用mapper接口查询结果列表
//        Integer start = (page - 1) * pageSize;
//        List<Emp> rows =  empMapper.list(start,pageSize);
//        return new PageResult<Emp>(total,rows);
//    }



//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize, String name, String gender, LocalDate begin, LocalDate end) {
//        //设置分页参数（PageHelper）
//        PageHelper.startPage(page,pageSize);
//        List<Emp> empList = empMapper.list(name,gender,begin,end);
//        Page<Emp> p = (Page<Emp>) empList;
//        return new PageResult<Emp>(p.getTotal(),p.getResult());
//    }

    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        //设置分页参数（PageHelper）
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        List<Emp> empList = empMapper.list(empQueryParam);
        Page<Emp> p = (Page<Emp>) empList;
        return new PageResult<Emp>(p.getTotal(),p.getResult());
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        //保存员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        empMapper.insert(emp);
        //保存员工工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(e->{
                e.setEmpId(emp.getId());
            });
            empExprMapper.insertBatch(exprList);
        }
    }


//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize) {
//        PageHelper.startPage(page,pageSize);
//        List<Emp> empList = empMapper.listAll();
//        Page<Emp> p = (Page<Emp>) empList;
//        return new PageResult<Emp>(p.getTotal(),p.getResult());
//    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Integer> ids) {
        empMapper.deleteByIds(ids);

        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getById(id);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(Emp emp) {
        //根据id修改员工的基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //根据id修改员工工作经历

        //1.先根据员工id删除原有的
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        //2.在添加新的工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr->empExpr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }
    }
}
